package com.printeverywhere.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.delegator.ItemsServiceDelegator;
import com.printeverywhere.dto.AuctionWonlist;
import com.printeverywhere.dto.ItemCategories;
import com.printeverywhere.dto.ItemNote;
import com.printeverywhere.dto.ItemOffers;
import com.printeverywhere.dto.Items;
import com.printeverywhere.dto.User;
import com.printeverywhere.util.SearchCriteria;
import com.printeverywhere.util.SessionUtil;

public class AddNoteAction extends FrontBaseSupport implements ServletRequestAware {

	private static final long serialVersionUID = 1L;

	public AddNoteAction() {
		super(true, false);
		setMyAccount(true);
	}


	private HttpServletRequest request;

	private ItemNote itemNote;

	private ItemOffers offers;

	private Items items;

	private String action;

	private String pageAction;

	private User user;

	AuctionWonlist wonlist;
	
	SearchCriteria searchCriteria = new SearchCriteria();
	
	List<ItemCategories> itemCategoriesList;

	public List<ItemCategories> getItemCategoriesList() {
		return itemCategoriesList;
	}

	public void setItemCategoriesList(List<ItemCategories> itemCategoriesList) {
		this.itemCategoriesList = itemCategoriesList;
	}

	public AuctionWonlist getWonlist() {
		return wonlist;
	}

	public void setWonlist(AuctionWonlist wonlist) {
		this.wonlist = wonlist;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public ItemNote getItemNote() {
		return itemNote;
	}

	public void setItemNote(ItemNote itemNote) {
		this.itemNote = itemNote;
	}

	public String execute() throws Exception {
		if (request.getParameter("p") != null) {
			pageAction = request.getParameter("p");
			if (request.getParameter("t") != null) {
				pageAction = pageAction.concat("?t="+request.getParameter("t"));
			}
		}

		if (request.getParameter("cid") != null) {
			if(request.getParameter("action") != null){
				if(request.getParameter("action").trim().equalsIgnoreCase("editnote")){
					SearchCriteria criteria = new SearchCriteria();
					criteria.removeAllAttribute();
					criteria.setAttribute("id", Integer.parseInt(request.getParameter("cid")));
					itemNote = (ItemNote) ((CommonFunctionDelegator.GeneralgetCriteriaList(ItemNote.class.getName(), criteria)).size() > 0 ? (CommonFunctionDelegator.GeneralgetCriteriaList(ItemNote.class.getName(), criteria)).get(0) : null);
					this.action = "updateNote.htm";
					searchCriteria.removeAllAttribute();
					searchCriteria.setAttribute("itemId", itemNote.getItems().getItemId());
					List<Items> itemList = (List<Items>) CommonFunctionDelegator.GeneralgetCriteriaList(Items.class.getName(), searchCriteria);
					if(itemList.size() > 0)
						items = itemList.get(0);
					if(items != null){
						searchCriteria.removeAllAttribute();
						searchCriteria.setAttribute("itemId", items);
						itemCategoriesList = (List<ItemCategories>) CommonFunctionDelegator.GeneralgetCriteriaList(ItemCategories.class.getName(), searchCriteria);
					}
				}else if(request.getParameter("action").trim().equalsIgnoreCase("addnote")){
					SearchCriteria criteria = new SearchCriteria();
					criteria.removeAllAttribute();
					criteria.setAttribute("itemId", Integer.parseInt(request.getParameter("cid")));
					items = (Items) ((CommonFunctionDelegator.GeneralgetCriteriaList(Items.class.getName(), criteria)).size() > 0 ? (CommonFunctionDelegator.GeneralgetCriteriaList(Items.class.getName(), criteria)).get(0) : null);
					searchCriteria.removeAllAttribute();
					searchCriteria.setAttribute("itemId", items);
					itemCategoriesList = (List<ItemCategories>) CommonFunctionDelegator.GeneralgetCriteriaList(ItemCategories.class.getName(), searchCriteria);
					this.action = "createNotes.htm";
				}
			}else if(request.getParameter("p") != null && (request.getParameter("p").equalsIgnoreCase("myWantedSold") || request.getParameter("p").equalsIgnoreCase("myTenderSold"))){
				SearchCriteria criteria = new SearchCriteria();
				criteria.removeAllAttribute();
				criteria.setAttribute("id", Integer.parseInt(request.getParameter("cid")));
				wonlist = (AuctionWonlist) CommonFunctionDelegator.GeneralgetCriteriaList(AuctionWonlist.class.getName(), criteria).get(0);
				items = wonlist.getItems();
				searchCriteria.removeAllAttribute();
				searchCriteria.setAttribute("itemId", items);
				itemCategoriesList = (List<ItemCategories>) CommonFunctionDelegator.GeneralgetCriteriaList(ItemCategories.class.getName(), searchCriteria);
				this.action = "createSoldNotes.htm";
			}
		}

		return SUCCESS;
	}

	public String addNote() throws Exception{
		System.out.println(pageAction);
		SearchCriteria criteria = new SearchCriteria();
		user = SessionUtil.getUser(request);
		criteria.removeAllAttribute();
		criteria.setAttribute("itemId", Integer.parseInt(request.getParameter("items.itemId")));
		items = (Items) ((CommonFunctionDelegator.GeneralgetCriteriaList(Items.class.getName(), criteria)).size() > 0 ? (CommonFunctionDelegator.GeneralgetCriteriaList(Items.class.getName(), criteria)).get(0) : null);
		itemNote = new ItemNote();
		itemNote.setItems(items);
		itemNote.setUser(user);
		String note = request.getParameter("itemNote.note");
		if(note.trim().length()> 0 )
		{
			itemNote.setNote(request.getParameter("itemNote.note"));
			CommonFunctionDelegator.generalInsertbyobj(itemNote);
		}
		return SUCCESS;
	}

	public String updateNote() throws Exception{
		SearchCriteria criteria = new SearchCriteria();
		criteria.removeAllAttribute();
		criteria.setAttribute("id", Integer.parseInt(request.getParameter("itemNote.id")));
		itemNote = (ItemNote) ((CommonFunctionDelegator.GeneralgetCriteriaList(ItemNote.class.getName(), criteria)).size() > 0 ? (CommonFunctionDelegator.GeneralgetCriteriaList(ItemNote.class.getName(), criteria)).get(0) : null);
		itemNote.setNote(request.getParameter("itemNote.note"));
		CommonFunctionDelegator.generalUpdatebyobj(itemNote);
		return SUCCESS;
	}

	public String addOfferNotes() throws Exception{
		if (request.getParameter("p") != null) {
			pageAction = request.getParameter("p");
		}
		if (request.getParameter("cid") != null) {
			SearchCriteria criteria = new SearchCriteria();
			criteria.removeAllAttribute();
			criteria.setAttribute("id", Integer.parseInt(request.getParameter("cid")));
			offers = (ItemOffers) ((CommonFunctionDelegator.GeneralgetCriteriaList(ItemOffers.class.getName(), criteria)).size() > 0 ? (CommonFunctionDelegator.GeneralgetCriteriaList(ItemOffers.class.getName(), criteria)).get(0) : null);
			this.action = "updateOfferNote.htm";
		}
		return  SUCCESS;
	}

	public String updateOfferNote() throws Exception{
		SearchCriteria criteria = new SearchCriteria();
		criteria.removeAllAttribute();
		criteria.setAttribute("id", Integer.parseInt(request.getParameter("offers.id")));
		offers = (ItemOffers) ((CommonFunctionDelegator.GeneralgetCriteriaList(ItemOffers.class.getName(), criteria)).size() > 0 ? (CommonFunctionDelegator.GeneralgetCriteriaList(ItemOffers.class.getName(), criteria)).get(0) : null);
		offers.setNote(request.getParameter("offers.note"));
		CommonFunctionDelegator.generalUpdatebyobj(offers);
		return SUCCESS;
	}

	public String createSoldNotes() throws Exception{
		if (request.getParameter("wonlist.id") != null) {
			SearchCriteria criteria = new SearchCriteria();
			criteria.removeAllAttribute();
			criteria.setAttribute("id", Integer.parseInt(request.getParameter("wonlist.id")));
			wonlist = (AuctionWonlist) CommonFunctionDelegator.GeneralgetCriteriaList(AuctionWonlist.class.getName(), criteria).get(0);
			this.wonlist.setNote(request.getParameter("wonlist.note"));
			try{
				ItemsServiceDelegator.addNote(wonlist);
				return SUCCESS;
			}catch (Exception e) {
				e.printStackTrace();
				return INPUT;
			}
		}
		return SUCCESS;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public Items getItems() {
		return items;
	}

	public void setItems(Items items) {
		this.items = items;
	}

	public String getPageAction() {
		return pageAction;
	}

	public void setPageAction(String pageAction) {
		this.pageAction = pageAction;
	}

	public ItemOffers getOffers() {
		return offers;
	}

	public void setOffers(ItemOffers offers) {
		this.offers = offers;
	}

}
